package view.window;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import controller.data.PlayerFile;
import model.prop.key.Key;
import model.role.player.Player;

public class InofView extends JPanel {

	private static final long serialVersionUID = 4486547097598241931L;

	private static final ImageIcon IMG = new ImageIcon("src\\model\\window\\inofBack.jpg");

	private Player player;

	private JButton exit;

	public InofView(Player player) {
		ImageIcon icon = new ImageIcon("src/model/window/button.png");
		Image instance = icon.getImage().getScaledInstance(180, 60, Image.SCALE_REPLICATE);
		ImageIcon icon2 = new ImageIcon(instance);
		exit = new JButton("保存并退出", icon2);
		exit.setHorizontalTextPosition(SwingConstants.CENTER);
		exit.setContentAreaFilled(false);// 设置图片填满按钮所在的区域
		exit.setBorderPainted(false);// 设置是否绘制边框
		exit.setBorder(null);// 设置边框
		exit.setFont(new Font("华文隶书", Font.PLAIN, 26));
		exit.setForeground(Color.BLACK);
		exit.addMouseListener(new MoveColor(exit));
		exit.addActionListener(e -> {
			PlayerFile.savePlayer(player);
			System.exit(0);
		});

		setLayout(new GridLayout(0, 1));
		this.player = player;
		setBounds(48, 48, 48 * 4, 48 * 11);
		update();
	}

	public void update() {
		removeAll();
		JLabel label1 = new JLabel(String.format("第%d层", player.getNowFloor()), JLabel.CENTER);
		label1.setFont(new Font("华文行楷", Font.BOLD, 50));
		add(label1);

		Font font = new Font("华文隶书", Font.BOLD, 30);

		JLabel label2 = new JLabel(String.format("生命：%d", player.getRed()), JLabel.CENTER);
		label2.setFont(font);
		add(label2);

		JLabel label3 = new JLabel(String.format("攻击：%d", player.getAttack()), JLabel.CENTER);
		label3.setFont(font);
		add(label3);

		JLabel label4 = new JLabel(String.format("防御：%d", player.getDefense()), JLabel.CENTER);
		label4.setFont(font);
		add(label4);

		JLabel label5 = new JLabel(String.format("金币：%d", player.getMoney()), JLabel.CENTER);
		label5.setFont(font);
		add(label5);

		JLabel label6 = new JLabel(String.format("经验：%d", player.getExp()), JLabel.CENTER);
		label6.setFont(font);
		add(label6);

		JLabel label7 = new JLabel("黄钥匙：" + (player.isVip() ? "∞" : player.getKeySize(Key.KEY_TYPE_YELLOW)),
				JLabel.CENTER);
		label7.setFont(font);
		add(label7);

		JLabel label8 = new JLabel("蓝钥匙：" + (player.isVip() ? "∞" : player.getKeySize(Key.KEY_TYPE_BLUE)),
				JLabel.CENTER);
		label8.setFont(font);
		add(label8);

		JLabel label9 = new JLabel("红钥匙：" + (player.isVip() ? "∞" : player.getKeySize(Key.KEY_TYPE_RED)),
				JLabel.CENTER);
		label9.setFont(font);
		add(label9);

		JLabel label10 = new JLabel(player.isVip() ? "VIP" : "", JLabel.CENTER);
		label10.setFont(font);
		label10.setForeground(Color.RED);
		add(label10);

		add(exit);
		setVisible(true);
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(IMG.getImage(), 0, 0, getWidth(), getHeight(), 0, 0, IMG.getIconWidth(), IMG.getIconHeight(), null);
	}

	private class MoveColor implements MouseListener {

		private JButton b;

		public MoveColor(JButton b) {
			super();
			this.b = b;
		}

		@Override
		public void mouseClicked(MouseEvent e) {

		}

		@Override
		public void mousePressed(MouseEvent e) {

		}

		@Override
		public void mouseReleased(MouseEvent e) {

		}

		@Override
		public void mouseEntered(MouseEvent e) {
			b.setForeground(Color.RED);
		}

		@Override
		public void mouseExited(MouseEvent e) {
			b.setForeground(Color.BLACK);
		}

	}
}
